/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.version.model;

import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import offset.nodes.Constants;

/**
 *
 * @author Walter L�tgenau
 */
public class FrozenNodePropertyIterator implements PropertyIterator{
    static final HashSet<String> ignoredProperties = new HashSet<String>();
    static {
        ignoredProperties.add(Constants.JCR_FROZEN_PRIMARY_TYPE);
        ignoredProperties.add(Constants.JCR_FROZEN_UUID);
        ignoredProperties.add(Constants.JCR_PREDECESSORS);
    }

    PropertyIterator iterator;
    int ignored = 0;

    public FrozenNodePropertyIterator(PropertyIterator iterator) {
        this.iterator = iterator;
    }

    @Override
    public void skip(long skipNum) {
        iterator.skip(skipNum);
    }

    @Override
    public long getSize() {
        return iterator.getSize() - ignoredProperties.size();
    }

    @Override
    public long getPosition() {
        return iterator.getPosition() - ignored;
    }

    @Override
    public Property nextProperty() {
        try {
            Property property = iterator.nextProperty();
            while (ignoredProperties.contains(property.getName())) {
                ignored++;
                property = iterator.nextProperty();
            }

            return property;
        } catch (RepositoryException ex) {
            Logger.getLogger(FrozenNodePropertyIterator.class.getName()).log(Level.SEVERE, null, ex);
            throw new NoSuchElementException();
        }
    }

    @Override
    public boolean hasNext() {
        return iterator.getSize() - iterator.getPosition() - ignoredProperties.size() > 0;
    }

    @Override
    public Object next() {
        return nextProperty();
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
